package com.example.shoottanks;

import java.util.ArrayList;

import javax.microedition.khronos.opengles.GL10;

import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.engine.camera.BoundCamera;
import org.anddev.andengine.engine.camera.hud.controls.BaseOnScreenControl;
import org.anddev.andengine.engine.camera.hud.controls.BaseOnScreenControl.IOnScreenControlListener;
import org.anddev.andengine.engine.camera.hud.controls.DigitalOnScreenControl;
import org.anddev.andengine.engine.options.EngineOptions;
import org.anddev.andengine.engine.options.EngineOptions.ScreenOrientation;
import org.anddev.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.anddev.andengine.entity.layer.tiled.tmx.TMXLayer;
import org.anddev.andengine.entity.layer.tiled.tmx.TMXProperties;
import org.anddev.andengine.entity.layer.tiled.tmx.TMXProperty;
import org.anddev.andengine.entity.layer.tiled.tmx.TMXTile;
import org.anddev.andengine.entity.layer.tiled.tmx.TMXTileProperty;
import org.anddev.andengine.entity.layer.tiled.tmx.TMXTiledMap;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.scene.background.ColorBackground;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.entity.util.FPSLogger;
import org.anddev.andengine.input.touch.TouchEvent;
import org.anddev.andengine.opengl.texture.Texture;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
import org.anddev.andengine.opengl.texture.region.TextureRegionFactory;
import org.anddev.andengine.ui.activity.BaseGameActivity;

import android.os.Bundle;
import android.widget.Toast;

import com.example.common.Constants;
import com.example.map.MyTitleMap;
import com.example.spritedto.TankDTO;

public class MainActivity extends BaseGameActivity {
	private BoundCamera mCamera;
	private Scene mScene;
	
	private TMXTiledMap tmxTiledMap;
	private TMXLayer vatCanLayer;
	
	private DigitalOnScreenControl screenControl;
	private Texture controlTexture;
	private TextureRegion controlBaseRegion;
	private TextureRegion controlKnobRegion;
	
	private TankDTO tankDTO;
	
	private Texture shootTexture;
	private TextureRegion shootRegion;
	private Sprite shootSprite;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public Engine onLoadEngine() {
		mCamera = new BoundCamera(0, 0, Constants.screenWidth, Constants.screenHeight);
		Engine mEngine = new Engine(new EngineOptions(true, ScreenOrientation.LANDSCAPE, new RatioResolutionPolicy(Constants.screenWidth, Constants.screenHeight), mCamera));
		tankDTO = new TankDTO(100, 10);
		return mEngine;
	}

	@Override
	public void onLoadResources() {
		TextureRegionFactory.setAssetBasePath("gfx/control/");
		//load bo dieu khien
		controlTexture = new Texture(256, 256, TextureOptions.BILINEAR);
		controlBaseRegion = TextureRegionFactory.createFromAsset(controlTexture, this, "ControlBase.png", 0, 0);
		controlKnobRegion = TextureRegionFactory.createFromAsset(controlTexture, this, "ControlKnob.png", 128, 0);
		mEngine.getTextureManager().loadTexture(controlTexture);
		//load tank
		tankDTO.onLoadResources(mEngine, this);
		//laod nut ban 
		shootTexture = new Texture(64, 64, TextureOptions.BILINEAR);
		shootRegion = TextureRegionFactory.createFromAsset(shootTexture, this, "ic_shoot.png", 0, 0);
		mEngine.getTextureManager().loadTexture(shootTexture);
	}

	@Override
	public Scene onLoadScene() {
		mEngine.registerUpdateHandler(new FPSLogger());
		mScene = new Scene();
		mScene.setBackground(new ColorBackground(0, 0, 0));
		//hien thi map
		tmxTiledMap = MyTitleMap.getTMXTitleMap(this, mEngine, Constants.mapName);
		ArrayList<TMXLayer> tmxLayers = tmxTiledMap.getTMXLayers();
		for(TMXLayer tmxLayer : tmxLayers){
			if(tmxLayer.getName().equals("tuong")){
				vatCanLayer = tmxLayer;
			}
			mScene.attachChild(tmxLayer);
		}		
		
		//hien thi mot cai tank
		tankDTO.onLoadScene(mScene);
		
		//mo rong camera
		TMXLayer tmxLayer = tmxTiledMap.getTMXLayers().get(0);
		mCamera.setBounds(0, tmxLayer.getWidth(), 0, tmxLayer.getHeight());
		mCamera.setBoundsEnabled(true);
		mCamera.setChaseEntity(tankDTO.tankSprite);
		
		//hien thi nut ban
		shootSprite = new Sprite(Constants.screenWidth - shootRegion.getWidth(), Constants.screenHeight - shootRegion.getHeight(), shootRegion){
			@Override
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY) {
				if(pSceneTouchEvent.getAction() == TouchEvent.ACTION_DOWN){
					Toast.makeText(getApplicationContext(), "tach", 1).show();
				}
				return true;
			}
		};
		shootSprite.setAlpha(0.7f);
		
		//hien thi bo dieu khien
		screenControl = new DigitalOnScreenControl(0, Constants.screenHeight - controlBaseRegion.getHeight(), mCamera, controlBaseRegion, controlKnobRegion, 0.1f, new IOnScreenControlListener() {
			
			@Override
			public void onControlChange(BaseOnScreenControl pBaseOnScreenControl,
					float pValueX, float pValueY) {
				tankDTO.updateStatus(pValueX, pValueY);
				if(pValueX != 0 || pValueY != 0){
					TMXTile tmxTile = vatCanLayer.getTMXTileAt(tankDTO.getPX(), tankDTO.getPY());
					try{
						if(tmxTile != null){
							TMXProperties<TMXTileProperty> tmxProperties = tmxTile.getTMXTileProperties(tmxTiledMap);
							TMXProperty tmxProperty = tmxProperties.get(0);
							if(tmxProperty.getName().equals("vatcan")){
								tankDTO.moveRelativeXY(0, 0);
							}
						}
					}catch (Exception e) {
						tankDTO.moveRelativeXY(pValueX, pValueY);
					}
				}
			}
		});
		screenControl.getControlBase().setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
		screenControl.getControlBase().setAlpha(0.7f);
		screenControl.getControlBase().setScaleCenter(0, 128);
		screenControl.getControlBase().setScale(0.8f);
		screenControl.getControlKnob().setScale(0.8f);
		screenControl.attachChild(shootSprite);
		screenControl.setTouchAreaBindingEnabled(true);
		screenControl.registerTouchArea(shootSprite);
		screenControl.refreshControlKnobPosition();

		mScene.setChildScene(screenControl);
		return mScene;
	}

	@Override
	public void onLoadComplete() {
		// TODO Auto-generated method stub	
	}

}
